package org.p2psimulator.entity.overlaynetwork;


import org.p2psimulator.engine.Environment;
import org.p2psimulator.entity.Id;
import org.p2psimulator.entity.OverlayNetwork;
import org.p2psimulator.entity.overlaynetwork.routingtable.ChordFingerTable;
import org.p2psimulator.entity.overlaynetwork.routingtable.GnutellaRoutingTable;
import org.p2psimulator.entity.overlaynetwork.routingtable.HibridRoutingTable;
import org.p2psimulator.entity.overlaynetwork.routingtable.RoutingTable;
import org.p2psimulator.exceptions.OverlayNetworkException;

public class HibridOverlay extends OverlayNetwork {


	ChordOverlay chordOverlay;
	
	GnutellaOverlay gnutellaOverlay;
	
	public HibridOverlay(Environment env) {
		super(env);
		chordOverlay = new ChordOverlay(env);
		gnutellaOverlay = new GnutellaOverlay(env);
	}
	
	@Override
	public void generateOverlay() throws OverlayNetworkException {
		gnutellaOverlay.generateOverlay();
		chordOverlay.generateOverlay();
	}

	@Override
	public RoutingTable getRoutingTable(Id idPeer)
			throws OverlayNetworkException {
		
		return new HibridRoutingTable(
				(ChordFingerTable)     chordOverlay.getFingerTable(idPeer), 
				(GnutellaRoutingTable) gnutellaOverlay.getRoutingTable(idPeer));
	}

	@Override
	public void joinNetwork(Id idPeer) throws OverlayNetworkException {
		this.gnutellaOverlay.joinNetwork(idPeer);
		this.chordOverlay.joinNetwork(idPeer);
	}

	@Override
	public void leaveNetwork(Id idPeer) {
		this.gnutellaOverlay.leaveNetwork(idPeer);
		this.chordOverlay.leaveNetwork(idPeer);
	}

	@Override
	public String printOverlay() {
		String str = this.gnutellaOverlay.printOverlay();
		str += "\n" + this.chordOverlay.printOverlay();
		return str;
	}

	public ChordOverlay getChordOverlay() {
		return chordOverlay;
	}

	public void setChordOverlay(ChordOverlay chordOverlay) {
		this.chordOverlay = chordOverlay;
	}

	public GnutellaOverlay getGnutellaOverlay() {
		return gnutellaOverlay;
	}

	public void setGnutellaOverlay(GnutellaOverlay gnutellaOverlay) {
		this.gnutellaOverlay = gnutellaOverlay;
	}

	
}
